﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;
using KeaoMDS.InfoManage;

namespace KeaoMDS.Weight
{
    public partial class WeiWanChengXiuGai : BaseForm
    {
        private DataGridViewRow m_SelectRow = null;
        private string m_ChengZhongLeiXing = string.Empty;
        private string m_HuanSuanGongShi = string.Empty;
        private string m_SheZhiQuXiang = string.Empty;
        private decimal m_MoRenKouZhong = 0m;
        private string m_KouZhongFangShi = string.Empty;
        private string m_ChengZhongID = string.Empty;
        private string m_XiuGaiQianZhongLiang = string.Empty;
        public WeiWanChengXiuGai(DataGridViewRow SelectRow,string ChengZhongLeiXing)
        {
            InitializeComponent();
            m_SelectRow = SelectRow;
            m_ChengZhongLeiXing = ChengZhongLeiXing;

            BindWuLiao();
            BindCheLiang();
            BindDanWei();
            BindYunShuGongSi();

        }

        private void WeiWanChengXiuGai_Load(object sender, EventArgs e)
        {
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("重量调整"))
            {
                txt_maozhong.Enabled = false;
            }

            if (m_ChengZhongLeiXing == "入库物料")
            {
                m_ChengZhongID = string.Format("{0}", m_SelectRow.Cells["weiwancheng_ruku_id"].Value);
                this.Text = "入库物料信息修改";
                txt_chepaihao.Text = string.Format("{0}", m_SelectRow.Cells["weiwancheng_ruku_chepaihao"].Value);
                txt_maozhong.Text = string.Format("{0}", m_SelectRow.Cells["weiwancheng_ruku_yicizhongliang"].Value);
                cmb_wuliao.Text = string.Format("{0}",m_SelectRow.Cells["weiwancheng_ruku_wuliaomingcheng"].Value);
                cmb_xinghao.Text = string.Format("{0}",m_SelectRow.Cells["weiwancheng_ruku_xinghao"].Value);
                cmb_danweimingcheng.Text = string.Format("{0}", m_SelectRow.Cells["weiwancheng_ruku_danweimingcheng"].Value);
                txt_fahuozhongliang.Text = string.Format("{0}", m_SelectRow.Cells["weiwancheng_ruku_fahuozhongliang"].Value);
                cmb_yunshugongsi.Text = string.Format("{0}", m_SelectRow.Cells["weiwancheng_ruku_yunshugongsi"].Value);
            }
            else
            {
                m_ChengZhongID = string.Format("{0}", m_SelectRow.Cells["weiwancheng_chuku_id"].Value);
                this.Text = "出库物料信息修改";
                txt_chepaihao.Text = string.Format("{0}", m_SelectRow.Cells["weiwancheng_chuku_chepaihao"].Value);
                txt_maozhong.Text = string.Format("{0}", m_SelectRow.Cells["weiwancheng_chuku_yicizhongliang"].Value);
                cmb_wuliao.Text = string.Format("{0}", m_SelectRow.Cells["weiwancheng_chuku_wuliaomingcheng"].Value);
                cmb_xinghao.Text = string.Format("{0}", m_SelectRow.Cells["weiwancheng_chuku_xinghao"].Value);
                cmb_danweimingcheng.Text = string.Format("{0}", m_SelectRow.Cells["weiwancheng_chuku_danweimingcheng"].Value);
                txt_fahuozhongliang.Text = string.Format("{0}", m_SelectRow.Cells["weiwancheng_chuku_fahuozhongliang"].Value);
                cmb_yunshugongsi.Text = string.Format("{0}", m_SelectRow.Cells["weiwancheng_chuku_yunshugongsi"].Value);
            }

            m_XiuGaiQianZhongLiang = txt_maozhong.Text;

        }

        private void menu_btn_baocun_Click(object sender, EventArgs e)
        {
            //保存数据

            if (string.IsNullOrEmpty(txt_chepaihao.Text) || string.IsNullOrEmpty(txt_maozhong.Text) || string.IsNullOrEmpty(cmb_danweimingcheng.Text) || string.IsNullOrEmpty(cmb_wuliao.Text))
            {
                KeaoUtils.ShowInfoMessage("信息不完整，请检查重试。");
                return;
            }
            if (!KeaoUtils.isNumber(txt_maozhong.Text))
            {
                KeaoUtils.ShowInfoMessage("重量必须为数字，请检查重试。");
                return;
            }
            decimal jingZhong = Convert.ToDecimal(txt_maozhong.Text);
            if (jingZhong <= 0m)
            {
                KeaoUtils.ShowInfoMessage("重量不正确，请检查重试。");
                return;
            }
            decimal faHuoZhongLiang = 0m;
            if (string.IsNullOrEmpty(txt_fahuozhongliang.Text) || !KeaoUtils.isNumber(txt_fahuozhongliang.Text))
            {
                faHuoZhongLiang = 0.00m;
            }
            else
            {
                faHuoZhongLiang = Convert.ToDecimal(txt_fahuozhongliang.Text);
            }

            TableRow editRow = new TableRow("tbl_chengzhonglinshi", m_ChengZhongID, DBCtl.修改);
            editRow.DataCols.Add(new TableCol() { ColName = "chepaihao", ValueMember = txt_chepaihao.Text, ValueType = DataValueCommonType.字符串 });
            editRow.DataCols.Add(new TableCol() { ColName = "danweimingcheng", ValueMember = cmb_danweimingcheng.Text, ValueType = DataValueCommonType.字符串 });
            editRow.DataCols.Add(new TableCol() { ColName = "wuliaomingcheng", ValueMember = cmb_wuliao.Text, ValueType = DataValueCommonType.字符串 });
            editRow.DataCols.Add(new TableCol() { ColName = "xinghao", ValueMember = cmb_xinghao.Text, ValueType = DataValueCommonType.字符串 });
            editRow.DataCols.Add(new TableCol() { ColName = "yicizhongliang", ValueMember = jingZhong.ToString("0.00"), ValueType = DataValueCommonType.数字 });
            editRow.DataCols.Add(new TableCol() { ColName = "huansuangongshi", ValueMember = m_HuanSuanGongShi, ValueType = DataValueCommonType.字符串 });
            editRow.DataCols.Add(new TableCol() { ColName = "shezhiquxiang", ValueMember = m_SheZhiQuXiang, ValueType = DataValueCommonType.字符串 });
            editRow.DataCols.Add(new TableCol() { ColName = "fahuozhongliang", ValueMember = faHuoZhongLiang.ToString("0.00"), ValueType = DataValueCommonType.数字 });
            editRow.DataCols.Add(new TableCol() { ColName = "morenkouzhong", ValueMember = m_MoRenKouZhong.ToString("0.00"), ValueType = DataValueCommonType.数字 });
            editRow.DataCols.Add(new TableCol() { ColName = "kouzhongfangshi", ValueMember = m_KouZhongFangShi, ValueType = DataValueCommonType.字符串 });
            editRow.DataCols.Add(new TableCol() { ColName = "yunshugongsi", ValueMember = cmb_yunshugongsi.SelectedIndex == 0 ? "" : cmb_yunshugongsi.Text, ValueType = DataValueCommonType.字符串 });
            if (KeaoUtils._DB.SaveTableRows(editRow) > 0)
            {
                MainForm.RiZhi(string.Format("修改未完成信息，车牌号【{2}】，原始重量【{0}】，修改后重量【{1}】", m_XiuGaiQianZhongLiang, txt_maozhong.Text,txt_chepaihao.Text));
                DialogResult = DialogResult.OK;
            }
            else
            {
                KeaoUtils.ShowInfoMessage("保存失败，请重试。");
            }

        }

        private void menu_btn_guanbi_Click(object sender, EventArgs e)
        {
            //关闭
            DialogResult = DialogResult.Cancel;
        }

        private void BindCheLiang()
        {
            string createCondition = KeaoUtils._DB.CreateAndCondition("zidongchengzhong", DataValueCommonType.字符串, "启用", "", SearchConditionType.等于);
            createCondition += KeaoUtils._DB.CreateAndCondition("cheliangleixing", DataValueCommonType.字符串, m_ChengZhongLeiXing=="入库物料" ? "入库车辆" : "出库车辆", "", SearchConditionType.等于);
            DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_cheliang", createCondition);
            txt_chepaihao.DisplayMember = "chepaihao";
            txt_chepaihao.ValueMember = "cheliangpizhong";
            txt_chepaihao.DataSource = Dt;
            txt_chepaihao.SelectedIndex = -1;
        }

        private void BindWuLiao()
        {
            try
            {
                string createCondition = KeaoUtils._DB.CreateAndCondition("wuliaoleixing", DataValueCommonType.字符串, m_ChengZhongLeiXing, "", SearchConditionType.等于);
                createCondition += KeaoUtils._DB.CreateAndCondition("wuliaozhuangtai", DataValueCommonType.字符串, "启用", "", SearchConditionType.等于);
                createCondition += KeaoUtils._DB.CreateAndCondition("wuliaoleixing", DataValueCommonType.字符串, m_ChengZhongLeiXing, "", SearchConditionType.等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_wuliao", createCondition);
                cmb_wuliao.DisplayMember = "wuliaomingcheng";
                cmb_wuliao.ValueMember = "id";
                cmb_wuliao.DataSource = Dt;
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("绑定物料信息异常。{0}", err.Message));
            }
        }

        /// <summary>
        /// 绑定型号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmb_wuliao_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable wuLiaoDt = cmb_wuliao.DataSource as DataTable;
                DataRow[] selectRows = wuLiaoDt.Select(string.Format("id='{0}'", cmb_wuliao.SelectedValue));
                m_SheZhiQuXiang = string.Format("{0}", selectRows[0]["shezhiquxiang"]);
              
                if (selectRows[0]["shezhifahuozhongliang"].Equals("启用"))
                {
                    txt_fahuozhongliang.Enabled = true;
                }
                else
                {
                    txt_fahuozhongliang.Enabled = false;
                }

                m_HuanSuanGongShi = string.Empty;
                string createCondition = KeaoUtils._DB.CreateAndCondition("wuliaobianhao", DataValueCommonType.字符串, string.Format("{0}", cmb_wuliao.SelectedValue), "", SearchConditionType.等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_xinghao", createCondition);
                cmb_xinghao.DisplayMember = "xinghao";
                cmb_xinghao.ValueMember = "id";
                cmb_xinghao.DataSource = Dt;
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("绑定型号信息异常。{0}", err.Message));
            }
        }

        /// <summary>
        /// 获取换算公式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void cmb_xinghao_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable Dt = cmb_xinghao.DataSource as DataTable;
            DataRow[] selectRow = Dt.Select(string.Format("id='{0}'", cmb_xinghao.SelectedValue));
            m_HuanSuanGongShi = string.Format("{0}", selectRow[0]["huansuangongshi"]);
            m_MoRenKouZhong =Convert.ToDecimal(selectRow[0]["morenkouzhong"]);
            m_KouZhongFangShi = string.Format("{0}", selectRow[0]["kouzhongfangshi"]);
        }

        private void BindDanWei()
        {
            try
            {
                string createCondition = KeaoUtils._DB.CreateAndCondition("danweileixing", DataValueCommonType.字符串, m_ChengZhongLeiXing=="入库物料" ? "入库单位" : "出库单位", "", SearchConditionType.等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_danwei", createCondition);
                cmb_danweimingcheng.DisplayMember = "danweimingcheng";
                cmb_danweimingcheng.ValueMember = "id";
                cmb_danweimingcheng.DataSource = Dt;
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("绑定单位信息异常。{0}", err.Message));
            }
        }

        private void BindYunShuGongSi()
        {
            string createCondition = KeaoUtils._DB.CreateAndCondition("gongsizhuangtai", DataValueCommonType.字符串, "启用", "", SearchConditionType.等于);
            DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_yunshugongsi", createCondition);
            cmb_yunshugongsi.DisplayMember = "gongsimingcheng";
            cmb_yunshugongsi.ValueMember = "id";
            DataRow newRow = Dt.NewRow();
            newRow["gongsimingcheng"] = "—请选择—";
            newRow["id"] = "";
            Dt.Rows.InsertAt(newRow, 0);
            cmb_yunshugongsi.DataSource = Dt;
        }

        private void btn_chepaihao_Click(object sender, EventArgs e)
        {
            CheLiangPaiHao cheLiangPaiHao = new CheLiangPaiHao();
            if (cheLiangPaiHao.ShowDialog() == DialogResult.OK)
            {
                txt_chepaihao.Text = cheLiangPaiHao.m_Current;
            }
        }

        private void txt_maozhong_Click(object sender, EventArgs e)
        {
            TextBox t = sender as TextBox;
            t.Focus();
            t.SelectAll();
        }
    }
}
